﻿(function () {
    $(function () {
        var _riskPointService =abp.services.app.companyInfo;
        var _$modal = $("#RiskPointCreateModal");
        var _$form = _$modal.find("form");

        //新增保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

         

            if (!_$form.valid()) {
                return;
            }
           
            var companyinfo = _$form.serializeFormToObject();

            //事故类型
            var workplacelist = "";
            
            var _$roleCheckboxes = $("input[name='workplacelist']:checked");
            if (_$roleCheckboxes) {
                for (var roleIndex = 0; roleIndex < _$roleCheckboxes.length; roleIndex++) {
                    var _$roleCheckbox = $(_$roleCheckboxes[roleIndex]);
                    workplacelist += _$roleCheckbox.val() + " ";
                }
            }
            if ($.trim(workplacelist) === "") {
                alert("请选择危险作业场所");
                return;
            }
            companyinfo.HazardousWorkPlaceTypes = $.trim(workplacelist);
            //提交
            abp.ui.setBusy(_$modal);           
            _riskPointService.createOrUpdateCompanyInfo({ companyinfo }).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost();
        });

        function refreshpost() {
            location.reload();
        }

       
        //编辑风险点
        $(".edit-companyinfo").click(function (e) {
            e.preventDefault();
            var companyinfoId = $(this).attr("data-companyinfo-id"); 

            $.ajax({
                url: abp.appPath + 'CompanyInfo/EditCompanyInfoModal?companyinfoId=' + companyinfoId,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#RiskPointEditModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });

        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();